package com.example.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.model.dtos.PageResult;
import com.example.model.dtos.R;
import com.example.model.dtos.UserDTO;
import com.example.model.pojo.User;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 用户控制类
 */
@RestController
@RequestMapping("user/")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 登录
     */
    @RequestMapping("login_auth")
    public R<User> login(@RequestBody Map<String,String> map, HttpSession httpSession){
        String username = map.get("username");
        String password = map.get("password");

        User user = userService.login(username,password);
        if (user==null){
            return R.error("用户名或密码错误");
        }
//        将用户信息存入session中，已备登录验证
        httpSession.setAttribute("user", user);
        user.setUserPassword("");
        return R.success(user);
    }

    /**
     * 添加用户
     */
    @RequestMapping("addUser")
    public R<String> addUser(@RequestBody Map<String,String> map){
        String username = map.get("username");
        String password = map.get("password");

        //判断参数是否为空
        if (StringUtils.isBlank(username)||StringUtils.isBlank(password)){
            return R.error("添加失败");
        }

        Boolean flag = userService.addUser(username,password);
        if (!flag){
            return R.error("添加失败");
        }

        return R.success("添加成功");
    }

    /**
     * 删除用户
     */
    @RequestMapping("deleteUser")
    public R<String> deleteUser(@RequestBody Map<String,String> map){
        String id = map.get("myId");
        //判断参数是否为空
        if (StringUtils.isBlank(id)){
            return R.error("参数错误");
        }

        Boolean flag = userService.deleteUser(id);
        if (!flag){
            return R.error("删除失败");
        }

        return R.success("删除成功");
    }

    /**
     * 修改用户权限
     */
    @RequestMapping("updateUser")
    public R<String> updateUser(@RequestBody Map<String,String> map){
        String id = map.get("myId");
        //判断参数是否为空
        if (StringUtils.isBlank(id)){
            return R.error("参数错误");
        }
        String inWarehouse = map.get("inWarehouse");
        String outWarehouse = map.get("outWarehouse");
        String goods = map.get("goods");
        String statistics = map.get("statistics");
        String basic = map.get("basic");
        String system = map.get("system");
        User user=new User();
        user.setMyId(id);
        user.setInWarehouse(inWarehouse);
        user.setOutWarehouse(outWarehouse);
        user.setGoods(goods);
        user.setStatistics(statistics);
        user.setBasic(basic);
        user.setSystem(system);




        Boolean flag = userService.updateUser(user);
        if (!flag){
            return R.error("更新成功");
        }

        return R.success("更新成功");
    }

    /**
     * 查看用户列表
     */
    @RequestMapping("UserList")
    public R<Object> UserList(@RequestBody Map<String,String> map){
        String pageStr = map.get("current");
        String pageSizeStr = map.get("size");
//        若为空，则查询第一页，每页10条
        if (StringUtils.isBlank(pageStr)){
            pageStr="1";
        }
        if (StringUtils.isBlank(pageSizeStr)){
            pageSizeStr="10";
        }
        Integer page = Integer.parseInt(pageStr);
        Integer pageSize = Integer.parseInt(pageSizeStr);


        PageResult pageResult = userService.userList(page,pageSize);
        return R.success(pageResult);
    }

}
